/*
 * The MIT License
 *
 * Copyright 2012 Hudson.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package hudson.msservice.timeremail;

import hudson.logging.HudsonTestLog;
import hudson.msservice.Messages;
import hudson.msservice.messageservice.MessageServiceProxy;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

/**
 * get plan's all jobs execute information then 
 * @author janksenhu
 */
public class EmailPlanJobExecuteInfo {
     
    /**
     * tools' execute infor send email to duty 
     */
    public static void sendPlanProjectsToAttention(){
        Map<String,Map<String,Object>> tem=null;
        Logger.getLogger(EmailToolsExecuteInfo.class.getName()).log(HudsonTestLog.level, " the plants job is "+tem);
        for(String plan_name:tem.keySet()){
            Map objs = tem.get(plan_name);
            List names =(List)objs.get("jobs");
            Map<String,Map<Integer,List>> map =null;
            Logger.getLogger(EmailToolsExecuteInfo.class.getName()).log(HudsonTestLog.level, " the map job is "+map);
            for(String projectname:map.keySet()){
                String to = (String)objs.get("attention");
                String from = "ptadmin@tencent.com";
                String cc = "";
                String bcc = "";
                String title = Messages.Email_Tools_Title(plan_name);
                String priority = MessageServiceProxy.MSG_PRIORITY_LOW;
                String bodyFormat = MessageServiceProxy.MSG_MAIL_BODYFORMAT_HTML;
                String emailType = MessageServiceProxy.MSG_MAIL_TYPE_ENCHANGE;
                List<String> attachFilePath = null;
                MessageServiceProxy instance = MessageServiceProxy.getInstance();

                Logger.getLogger(EmailToolsExecuteInfo.class.getName()).log(HudsonTestLog.level, " the head is "+Messages.Email_Tools_Head("http://10.6.193.204:8080/hudson"));
                String e_head=Messages.Email_Tools_Head("http://10.6.193.204:8080/hudson/");

                if(map.get(projectname).keySet().size()==0){
                    continue;
                }

                for(Integer buildnum : map.get(projectname).keySet()){
                    List l_b=map.get(projectname).get(buildnum);
                    String e_table = Messages.Email_Tools_Tables_Tr(projectname, buildnum, l_b.get(0), l_b.get(1), MessageUtil.getChineseOfResult((String)l_b.get(2)), MessageUtil.getTimeStringOfDuration((Integer)l_b.get(3)), l_b.get(4));
                    e_head+=e_table;
                }
                
                Logger.getLogger(EmailToolsExecuteInfo.class.getName()).log(HudsonTestLog.level, " the attention is "+to);
                
                String html_string = e_head+Messages.Email_Tools_Foot("http://10.6.193.204/ci");
                Logger.getLogger(EmailToolsExecuteInfo.class.getName()).log(HudsonTestLog.level, " the html is "+html_string);
                instance.sendMail(from, to, cc, bcc, title, html_string, priority, bodyFormat, emailType, attachFilePath);
            }               
            
        }
        
    }
    
    /**
     * implement the trigger's Job class method 
     */
    public void execute(JobExecutionContext jec) throws JobExecutionException {
           sendPlanProjectsToAttention();  
    }
}
